//
//  UserSexViewController.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/26.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserSexAndHandController.h"
#import "UserGroup.h"
#import "UserItem.h"
#import "UserSaveItem.h"
#import "UserCenterCell.h"

@interface UserSexAndHandController ()

/**
 *  当前选中的cell
 */
@property (nonatomic, strong)UserCenterCell *currentCell;

@end

@implementation UserSexAndHandController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    if (self.selectType == UserSelectSex) {
        [self loadSexData];
    }else if (self.selectType == UserSelectHabitHand){
        [self loadHabitHandData];
    }
    
    
}

- (void)loadSexData {
    UserGroup *group0 = [[UserGroup alloc] init];
    group0.headHeight = kAutoHei(24);
    
    UserSaveItem *item0_0 = [[UserSaveItem alloc] initWithTitle: NSLocalizedString(@"Male", nil)];
    UserSaveItem *item0_1 = [[UserSaveItem alloc] initWithTitle:NSLocalizedString(@"Female", nil)];
    [group0.items addObjectsFromArray:@[item0_0, item0_1]];
    
    [self.groups addObject:group0];
}


- (void)loadHabitHandData {
    
    UserGroup *group0 = [[UserGroup alloc] init];
    group0.headHeight = kAutoHei(24);
    
    UserSaveItem *item0_0 = [[UserSaveItem alloc] initWithTitle:NSLocalizedString(@"Left hander", nil)];
    UserSaveItem *item0_1 = [[UserSaveItem alloc] initWithTitle:NSLocalizedString(@"Right hander", nil)];
    
    [group0.items addObjectsFromArray:@[item0_0, item0_1]];
    
    [self.groups addObject:group0];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UserCenterCell *cell = [UserCenterCell settingViewCellTableView:tableView withStyle:UITableViewCellStyleValue1];
    
    UserGroup *group = self.groups[indexPath.section];
    UserSaveItem *item = group.items[indexPath.row];
    cell.item = item;
    
    if (self.currentCell == nil && [item.title isEqualToString:self.useSettingText]) {
        
        self.currentCell = cell;
        cell.selected = YES;
        [cell setSelected:YES animated:NO];
        
    }

    if (indexPath.row == group.items.count - 1) {
        [cell showLine:NO];
    }else {
        [cell showLine:YES];
    }
    if (indexPath.row == 0) {
        [cell showSectionSepTopLineView:YES];
    }else {
        [cell showSectionSepTopLineView:NO];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 取出对应的模型数据
    UserGroup *group = self.groups[indexPath.section];
    UserItem *item = group.items[indexPath.row];
    
    // 2.判断有没有block,有的话就执行
    if (item.block) {
        item.block();
        return;
    }
    UserCenterCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (self.currentCell) {
        self.currentCell.selected = NO;
    }
    
    cell.selected = YES;
    self.currentCell = cell;
    
    if (self.UserSexBlock) {
        self.UserSexBlock(self.currentCell.item.title);
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}


@end
